Tutorial

How to Install Node.js on Ubuntu (Step-by-Step Guide)

How to Install Node.js on Ubuntu (Step-by-Step Guide)
Not using Ubuntu 22.04?Choose a different version or distribution.
Ubuntu 22.04

Introduction

Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.

In this guide, we will show you four different ways of getting Node.js installed on an Ubuntu server:

  • using apt to install the nodejs package from Ubuntu’s default software repository
  • using apt with an alternate PPA software repository to install specific versions of the nodejs package
  • installing nvm, the Node Version Manager, and using it to install and manage multiple versions of Node.js
  • installing Node.js from source

For many users, using apt with the default repo will be sufficient. If you need specific newer (or legacy) versions of Node, you should use the PPA repository. If you are actively developing Node applications and need to switch between node versions frequently, choose the nvm method.

Simplify deploying Node applications with DigitalOcean App Platform. Deploy directly from GitHub in minutes.

Prerequisites

This guide assumes that you are using the latest Ubuntu version (at the time of writing, the latest Ubuntu lts version was 24.04). Before you begin, you should have a non-root user account with sudo privileges set up on your system. You can learn how to do this by following the right guide from this collection on Ubuntu initial server setup.

Before proceeding, we recommend you take a look at this nodesource distribution chart to check which Node version is compatible with your Ubuntu installation.

These steps are valid for the most recent versions of Ubuntu: Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04. If you are using Ubuntu version <= 18.04, we recommend you upgrade to a more latest version since Ubuntu no longer provides support for the older versions. This collection of guides will help you in upgrading your Ubuntu version.

Option 1 — Installing Node.js with Apt from the Default Repositories

Ubuntu contains a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is 12.22.9. This will not be the latest version, but it should be stable and sufficient for quick experimentation with the language.

To get this version, you can use the apt package manager. Refresh your local package index first by typing:

  1. sudo apt update

Then install Node.js:

  1. sudo apt install nodejs

Press Y when prompted to confirm installation. If you are prompted to restart any services, press ENTER to accept the defaults and continue. Check that the install was successful by querying node for its version number:

  1. node -v
Output
v18.19.1

If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In most cases, you’ll also want to install npm, the Node.js package manager. You can do this by installing the npm package with apt:

  1. sudo apt install npm

This will allow you to install modules and packages to use with Node.js.

At this point you have successfully installed Node.js and npm using apt and the default Ubuntu software repositories. The next section will show how to use an alternate repository to install different versions of Node.js.

Option 2 — Installing Node.js with Apt Using a NodeSource PPA

To install a different version of Node.js, you can use a PPA (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v14, v16, and v18 are available as of the time of writing.

First, we will install the PPA in order to get access to its packages. From your home directory, use curl to retrieve the installation script for your preferred version, making sure to replace 20.x with your preferred version string (if different).

If you do not have curl installed, you can install it by running sudo apt install curl.

  1. cd ~
  2. curl -sL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh

Refer to the NodeSource documentation for more information on the available versions.

You can inspect the contents of the downloaded script with nano (or your preferred text editor):

  1. nano nodesource_setup.sh

Running third party shell scripts is not always considered a best practice, but in this case, NodeSource implements their own logic in order to ensure the correct commands are being passed to your package manager based on distro and version requirements. If you are satisfied that the script is safe to run, exit your editor, then run the script with sudo:

  1. sudo bash nodesource_setup.sh

The PPA will be added to your configuration and your local package cache will be updated automatically. You can now install the Node.js package in the same way you did in the previous section. It may be a good idea to fully remove your older Node.js packages before installing the new version, by using sudo apt remove nodejs npm. This will not affect your configurations at all, only the installed versions. Third party PPAs don’t always package their software in a way that works as a direct upgrade over stock packages, and if you have trouble, you can always try to revert to a clean slate.

  1. sudo apt install nodejs

Verify that you’ve installed the new version by running node with the -v version flag:

  1. node -v
Output
v23.11.1

The NodeSource nodejs package contains both the node binary and npm, so you don’t need to install npm separately.

At this point you have successfully installed Node.js and npm using apt and the NodeSource PPA. The next section will show how to use the Node Version Manager to install and manage multiple versions of Node.js.

Option 3 — Installing Node Using the Node Version Manager

Another way of installing Node.js that is particularly flexible is to use nvm, the Node Version Manager. This piece of software allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.

To install NVM on your Ubuntu machine, visit the project’s GitHub page. Copy the curl command from the README file that displays on the main page. This will get you the most recent version of the installation script.

Before piping the command through to bash, it is always a good idea to audit the script to make sure it isn’t doing anything you don’t agree with. You can do that by removing the | bash segment at the end of the curl command:

  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh

Take a look and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with | bash appended at the end. The URL you use will change depending on the latest version of nvm, but as of right now, the script can be downloaded and executed by typing:

  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

This will install the nvm script to your user account. To use it, you must first source your .bashrc file:

  1. source ~/.bashrc

Now, you can ask NVM which versions of Node are available:

  1. nvm list-remote
Output
. . . v22.10.0 v22.11.0 (LTS: Jod) v22.12.0 (LTS: Jod) v22.13.0 (LTS: Jod) v22.13.1 (LTS: Jod) v22.14.0 (LTS: Jod) v22.15.0 (LTS: Jod) v22.15.1 (LTS: Jod) v22.16.0 (Latest LTS: Jod) v23.0.0 v23.1.0 v23.2.0 v23.3.0 v23.4.0 v23.5.0 v23.6.0 v23.6.1 v23.7.0 v23.8.0 v23.9.0 v23.10.0 v23.11.0 v23.11.1 v24.0.0 v24.0.1 v24.0.2 v24.1.0

It’s a very long list! You can install a version of Node by typing any of the release versions you see. For instance, to get version v22.16.0 (another LTS release), you can type:

  1. nvm install v22.16.0

You can see the different versions you have installed by typing:

  1. nvm list
Output
-> v22.16.0 default -> v22.16.0 iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v22.16.0) (default) stable -> 22.16 (-> v22.16.0) (default) lts/* -> lts/jod (-> v22.16.0) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.12 (-> N/A) lts/fermium -> v14.21.3 (-> N/A) lts/gallium -> v16.20.2 (-> N/A) lts/hydrogen -> v18.20.8 (-> N/A) lts/iron -> v20.19.2 (-> N/A) lts/jod -> v22.16.0

This shows the currently active version on the first line (-> v22.16.0), followed by some named aliase s and the versions that those aliases point to.

Note: if you also have a version of Node.js installed through apt, you may see a system entry here. You can always activate the system-installed version of Node using nvm use system.

You can install a release based on these aliases as well. For instance, to install fermium, run the following:

  1. nvm install lts/fermium
Output
Downloading and installing node v14.21.3... Downloading https://nodejs.org/dist/v14.21.3/node-v14.21.3-linux-x64.tar.xz... ################################################################################# 100.0% Computing checksum with sha256sum Checksums matched! Now using node v14.21.3 (npm v6.14.18)

You can verify that the install was successful using the same technique from the other sections, by typing:

  1. node -v
Output
v14.21.3

The correct version of Node is installed on our machine as we expected. A compatible version of npm is also available.

If you’ve followed the tutorial till now, you should have two versions (v14.21.3 and v22.16.0) of node installed in your system:

  1. nvm list
Output
-> v14.21.3 v22.16.0 default -> v22.16.0 iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v22.16.0) (default) stable -> 22.16 (-> v22.16.0) (default) lts/* -> lts/jod (-> v22.16.0) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.12 (-> N/A) lts/fermium -> v14.21.3 lts/gallium -> v16.20.2 (-> N/A) lts/hydrogen -> v18.20.8 (-> N/A) lts/iron -> v20.19.2 (-> N/A) lts/jod -> v22.16.0

The current active version is v14.21.3. To switch the version to v22.16.0, run the following command:

  1. nvm use v22.16.0
Output
Now using node v22.16.0 (npm v10.9.2)

You can confirm the current active version by running node -v or nvm current.

To set a default version that NVM will use every time you open a new shell:

  1. nvm alias default <version_number>

For example:

  1. nvm alias default v22.16.0
Output
default -> v22.16.0

This will set the default version to v22.16.0. Now whenever you open a new shell, this will be the default version.

Option 4 — Installing Node.js from source (for advanced users)

Although Ubuntu repositories and NodeSource offer simpler Node.js installation methods, building from source can be helpful in certain situations. These include requiring a version unavailable elsewhere, customizing compile-time features by enabling or disabling them, engaging in Node.js core development or applying custom patches, or utilizing particular performance optimization flags during compilation. This method is specifically recommended for advanced users.

First, let’s install the necessary build tools and dependencies:

  1. sudo apt update
  2. sudo apt install build-essential python3 g++ make pkg-config

Next, go to the official Node.js download page and find the “Source code” link for the version you want (usually a tar.gz). Next, open the terminal and type the following command:

  1. wget https://nodejs.org/dist/v20.19.2/node-v20.19.2.tar.gz

For this example, we’re downloading v20.19.2 but you can replace this with the version you want to download.

Once the tarball is downloaded, extract it using:

  1. tar -xzf node-v20.19.2.tar.gz

Navigate to the node-v20.19.2 folder and let’s configure the build. The ./configure script prepares the build environment:

  1. ./configure

This will install Node.js to /usr/local which is the default. If you want to install Node.js to a different location, you can use the --prefix option.

  1. ./configure --prefix=$HOME/.local/node-v20.19.2.0 # Or any path you prefer

We used $HOME/.local/node-v20.19.2 for this example, but you can choose any location you want.

Time to compile the source code:

  1. make

This step will take a significant amount of time depending on your system. If you want to speed up the compilation, you can use the -j flag followed by the number of processors you want to use for this build. You can use nproc to find the number of available cores.

  1. make -j$(nproc)

We can install Node.js now. Before that, it is a good practice to test the build to ensure everything was build correctly:

  1. make test

If the test fails, there might be an issue with the dependencies, environment, or the source code. Resolve the issues before moving on to the next step.

Finally, we can install Node.js by running the following command:

  1. sudo make install

If you configured the build to a different location from the default one, you can skip sudo and just run make install. Also, after installation, add the bin directory of your custom installation path to your system’s PATH environment variable so that your shell can find the node and npm executables. Open your shell configuration file (e.g., ~/.bashrc for Bash, ~/.zshrc for Zsh) with nano ~/.bashrc and add the following line at the end of the file:

export PATH="$HOME/.local/node-v20.19.2/bin:$PATH"

Save the file. Then apply the changes to your current terminal session:

  1. source ~/.bashrc

You can verify the installation by running:

  1. node -v
  2. npm -v

This will output the version of node and npm you just compiled.

Finally, you can remove the downloaded tarball and the extracted source directory to save disk space:

  1. cd .. # Go back from the node source directory
  2. rm node-v20.19.2.tar.gz # Replace with your version
  3. rm -rf node-v20.19.2 # Replace with your directory name

Removing Node.js

You can uninstall Node.js using apt or nvm, depending on how it was installed. To remove the version from the system repositories, use apt remove:

  1. sudo apt remove nodejs

By default, apt remove retains any local configuration files that were created since install. If you don’t want to save the configuration files for later use, use apt purge:

  1. sudo apt purge nodejs

To uninstall a version of Node.js that you installed using nvm, first determine whether it is the current active version:

  1. nvm current

If the version you are targeting is not the current active version, you can run:

  1. nvm uninstall node_version
Output
Uninstalled node node_version

This command will uninstall the selected version of Node.js.

If the version you would like to remove is the current active version, you’ll first need to deactivate nvm to enable your changes:

  1. nvm deactivate

Now you can uninstall the current version using the uninstall command used previously. This removes all files associated with the targeted version of Node.js.

FAQs

1. What’s the best way to install Node.js on Ubuntu?

The “best” way to install Node.js on Ubuntu depends on your specific needs:

  • For simplicity and stability (often not the latest version): Using Ubuntu’s default repositories is the easiest.

    1. sudo apt update
    2. sudo apt install nodejs npm

    However, the version provided by default Ubuntu repositories might not be the latest.

  • For newer versions of Node.js (Recommended for most users):

    Using NodeSource repositories is a popular and reliable method. NodeSource maintains up-to-date packages for various Node.js versions. First, you’ll add the NodeSource repository (replace 20.x with your desired major version, e.g., 18.x, 22.x, or current for the latest):

    1. curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

    Then, install Node.js:

    1. sudo apt install -y nodejs

    This method provides newer versions than the default Ubuntu repositories and integrates with the system’s package manager.

  • For managing multiple Node.js versions (Ideal for developers): Node Version Manager (NVM) is highly recommended. It allows you to install, switch between, and manage multiple Node.js versions on the same system without conflicts.

    Install NVM (the version number in the URL might change; check the official NVM GitHub page for the latest command):

    1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

    Source your shell configuration file (e.g., .bashrc, .zshrc) or open a new terminal.

    Install a specific Node.js version:

    1. nvm install 20 # Installs the latest v20 release
    2. nvm install --lts # Installs the latest Long-Term Support (LTS) version

    Use a specific version:

    1. nvm use 20

In summary, new users or those needing a specific recent version should use NodeSource. Developers working on multiple projects with different Node.js version requirements would benefit from using NVM.

2. Does Node.js come with npm?

Yes, npm (Node Package Manager) is included by default with Node.js installations. When you install Node.js using any of the common methods (official installer, NodeSource, NVM, etc.), npm will be installed alongside it. You can verify their installations by running:

  1. node -v
  2. npm -v

3. How do I update Node.js on Ubuntu?

The update process depends on how you initially installed Node.js:

  • If you installed Node.js via apt from Ubuntu’s default repositories or NodeSource:

    If you want to upgrade to a new major version (e.g., from Node.js 18.x to 20.x), you’ll first need to run the NodeSource setup script for the new desired version:

    1. curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

    Then, update your package list and upgrade the nodejs package:

    1. sudo apt update
    2. sudo apt install nodejs

    This command will upgrade Node.js (and npm) to the latest version available in the configured repository.

  • If you installed Node.js via NVM:

    To install the latest version of Node.js, run the following command:

    1. nvm install node

    Run the following to install the latest LTS version:

    1. nvm install --lts

    If you want to install a specific version (e.g., 20.10.0), you can use:

    1. nvm install 20.10.0

    After installing, you can switch to the new version using this command:

    1. nvm use <version_number_or_alias>

    For example: nvm use node or nvm use --lts.

    You can also set a default version:

    1. nvm alias default node

4. Can I have multiple Node.js versions installed?

Yes. This is a common requirement for developers. The best way to manage multiple Node.js versions on a single system is by using a version manager.

Node Version Manager (NVM) is the most popular tool for this. NVM allows you to install multiple Node.js versions side-by-side, switch between installed versions globally or per project, and set a default Node.js version.

Using NVM, you can list all the installed versions with nvm ls and switch between different versions using nvm use <version>.

5. Is it safe to install Node.js from NodeSource?

Yes, it is safe to install Node.js from NodeSource. NodeSource is a reputable company that specializes in Node.js and provides commercially supported Node.js platforms and tools. Their distributions are widely used.

The NodeSource setup scripts are open source and available on GitHub, so you can inspect them before running. The script’s primary function is to add the NodeSource APT repository and its signing key to your system’s package manager configuration. This allows apt to securely download and install Node.js packages signed by NodeSource. Adding any third-party PPA (Personal Package Archive) or repository involves a similar level of trust in the maintainer of that repository.

But always ensure you are getting the curl command from the official NodeSource website or documentation. If you are highly security-conscious, you can download the script first, review its contents, and then execute it locally. For most users and organizations, NodeSource provides a trusted and convenient way to install up-to-date versions of Node.js on Ubuntu.

Conclusion

There are quite a few ways to get up and running with Node.js on your Ubuntu server. Your circumstances will dictate which of the above methods is best for your needs. While using the packaged version in Ubuntu’s repository is the easiest method, using nvm or a NodeSource PPA offers additional flexibility.

For more information on programming with Node.js, please refer to our tutorial and resources:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Alex Garnett
Alex GarnettSenior DevOps Technical Writer
See author profile

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
7 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Bonjour, avec nvm j’ai un Failed Failed to download ‘https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/bash_completion

all the guides by digitalocean are great, THANKYOU, LOTS OF LOVE

I’m installing Ghost CMS and keep running into sudo: npm: command not found

Node installed fine under the normal user but Node is not available to root.

Is there something else that I’d need to do?

When the install script in “Option 2 — Installing Node.js with Apt Using a NodeSource PPA” is run, it says it deprecated and will not longer work in the future. Latest version of Node.js is 20.

Using the install instructions from the Node.js GitHub (https://github.com/nodesource/distributions/blob/master/README.md) works.

Steps to install 20.x:

  $ sudo apt-get update -y
  $ sudo apt-get install -y ca-certificates curl gnupg
  $ sudo mkdir -p /etc/apt/keyrings
  $ curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
  $ NODE_MAJOR=20
  $ echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
  $ sudo apt-get update
  $ sudo apt-get install -y nodejs
  $ node -v
v20.6.1
  $ npm -v
9.8.1

This guide provides detailed instructions for installing Node.js on an Ubuntu 22.04 server using three different methods. It starts with the basic method of using apt to install Node.js from the default repositories, suitable for most users who need a stable version. Then, it introduces the use of a NodeSource PPA to access more versions of Node.js, followed by an explanation of how to use nvm (Node Version Manager) for managing multiple Node.js versions.

Since nvm won’t install node environment to /usr/local/bin directory we must create symlinks in order to use node with services like GitHub Actions:

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"

This guide is outstanding! Your step-by-step instructions for installing Node.js on Ubuntu 22.04 were clear and easy to follow. I especially appreciated the helpful tips that made the process smoother. It’s great to have such detailed information readily available. Your effort in creating this content really shines through. Thanks for sharing your expertise it’s incredibly valuable! I’ll definitely refer back to this in the future.

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.